package service

import (
	"errors"
	"fake-handwriting-server/database"
	"fake-handwriting-server/model"
	"fake-handwriting-server/util"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func Register(ctx *gin.Context) {
	db := database.GetDatabase()
	mail := ctx.Query("mail")
	password := ctx.Query("password")
	var user model.User
	res := db.Where("mail = ?", "2506564278@qq.com").First(&user)
	if errors.Is(res.Error, gorm.ErrRecordNotFound) { //没找到，就可以注册
		db.Create(&model.User{Mail: mail, Password: password, Token: util.GenerateToken(
			"emmmmmmm",
			mail,
			password,
			"该用户未修改昵称",
		), Nickname: "该用户未修改昵称"})
		//送两个字体一个图片
		db.Create(&model.File{Mail: mail, Path: "assets/font/default.ttf", FileType: "font", Name: "default.ttf"})
		db.Create(&model.File{Mail: mail, Path: "assets/font/default1.ttf", FileType: "font", Name: "default1.ttf"})
		db.Create(&model.File{Mail: mail, Path: "assets/images/default_background.png", FileType: "images", Name: "default_background.png"})
		util.Back(ctx, true, "注册成功", gin.H{})
	} else {
		util.Back(ctx, false, "该邮箱已注册", gin.H{})
	}
}
